package com.zrkj.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zrkj.interceptor.UserContextHolder;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Classname MybatisObjectHandler
 * @Description TODO
 * @Date 2021/5/19 9:54
 * @Created 七分宁日
 */
@Component
public class MybatisObjectHandler implements MetaObjectHandler {

    private String getCurrentUserId() {
        return String.valueOf(UserContextHolder.getInstance().getUserId());
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        setInsertFieldValByName("createTime", LocalDateTime.now(),metaObject);
        setInsertFieldValByName("createBy", getCurrentUserId(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setUpdateFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        setUpdateFieldValByName("updateBy", getCurrentUserId(),metaObject);
    }
}
